<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%String ref = request.getHeader("REFERER");%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link href="../css/bookinfoadd.css"  rel="stylesheet" type="text/css">
    <script type="text/javascript" src="<%=request.getContextPath()%>/js/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            jQuery.jqtab = function(tabtit,tabcon) {
                $(tabcon).hide();
                $(tabtit+" li:first").addClass("thistab").show();
                $(tabcon+":first").show();

                $(tabtit+" li").click(function() {
                    $(tabtit+" li").removeClass("thistab");
                    $(this).addClass("thistab");
                    $(tabcon).hide();
                    var activeTab = $(this).find("a").attr("tab");
                    $("#"+activeTab).fadeIn();
                    return false;
                });

            };
            $.jqtab("#tabs",".tab_con");
            getBookSorts();
        });

        function getBookSorts(){
            $.getJSON("../getBookSorts?opr=sortList","",function(data){
                var sortDiv = $("#sortDiv").empty();
                $(data).each(function(){
                    var htmlStr = "<option value=\""+this.sort_id+"\">"+this.sort_name+"</option>";
                    sortDiv.append(htmlStr);
                })
            });
        }
        function checkBookInfo(){
            if($("#isbn").val()===""){
                alert("ISBN不能为空！");
                return false;
            }else if($("#bookName").val()===""){
                alert("书籍名称不能为空！");
                return false;
            }else if($("#bookAuthor").val()===""){
                alert("书籍著者不能为空！");
                return false;
            }else if($("#bookPublisher").val()===""){
                alert("出版单位不能为空！");
                return false;
            }else if($("#bookNums").val()===""){
                alert("书籍数量不能为空！");
                return false;
            }
            return true;
        }
        function addBook(){
            if(checkBookInfo()){
                var $inputs = $("#basicInfo").find("input,select");
                //只序列化 表单的数据 file处理不了
                var queryString = $inputs.serialize(); //{"":"","":"" }   request.getParameter("name")
                $.getJSON("../getBooks?opr=addBook",queryString,function(data){
                    //alert(data.message);//success failed
                    if (data>0){
                        alert("新增成功");
                    }else{
                        alert("新增失败");
                    }
                });
            }
        }

        function updateBookAbstract(){  //改摘要  book_abstarct的这个字段
            if(isbn != ""){
                $.getJSON("../updateBook?opr=abstract",{"isbn":$("#isbn").val(),"abstract":$("#abstract").val()},function(data){
                    alert(data.message);
                });
            }else{
                alert("请先添加书籍基本信息！");
            }
        }

        function goBack(){
            window.location='<%=ref%>';
        }

        function updateBookPhoto(){ //改图片 书籍封面照片

            var myFormData = new FormData();
            myFormData.append("isbn",$("#isbn").val());//表单数据
            myFormData.append("fileField",$("#fileField")[0].files[0]); //file的文件

            $.ajax({
                type:'post',
                dataType:'json',
                url:'../updateBook?opr=photo',
                contentType:false,
                processData:false,
                data:  myFormData,
                success: function(data){
                    if(data.result==1){
                        var photoFile =data.photo;
                        $("#photo").attr('src',photoFile);
                    }
                },
                error: function(data,type,err){
                    console.log("ajax错误类型："+type);
                    console.log(err);
                }
            });

        }
    </script>
    <title>书籍信息新增修改</title>
</head>

<body style="background:#fff;">
<div id="tabbox">
    <ul class="tabs" id="tabs">
        <li><a href="#" tab="tab1">基本信息</a></li>
        <li><a href="#" tab="tab2">附加信息</a></li>
        <li><a href="#" tab="tab3">图片信息</a></li>
    </ul>
    <ul class="tab_conbox">
        <li id="tab1" class="tab_con">
            <div id="father">
                <div id="mather">

                <form method="post" id="basicInfo">
                    <div class="four"><strong>ISBN</strong></div>
                    <div class="five"><input type="text" name="isbn" id="isbn" size="30"/></div>
                    <br/><br/><br/>
                    <hr/>
                    <div class="first">书籍名称<strong>*</strong></div>
                    <div id="first"><input type="text" name="bookName" id="bookName" size="35" value="曹良玉环游世界100天"/></div>
                    <div class="second">书籍著者<strong>*</strong></div>
                    <div id="second"><input type="text" name="bookAuthor" id="bookAuthor" size="35" value="曹阿玉"/></div>
                    <div class="thead">出版单位<strong>*</strong></div>
                    <div id="thead"><input type="text" name="bookPublisher" id="bookPublisher" size="35" value="科泰教育出版社"/></div>
                    <div class="fouth">出版时间</div>
                    <div id="fouth"><input type="text" name="bookPubYear" id="bookPubYear" size="35" value="2022"/></div>
                    <div class="fifth">类别<strong>*</strong></div>
                    <div id="fifth"><select id="sortDiv" name="bookSort" ></select></div>
                    <div class="sexth">发行地</div>
                    <div id="sexth"><input type="text" name="bookPubLocation" id="bookPubLocation" size="35" value="湖南"/></div>
                    <div class="seventh">数量<strong>*</strong></div>
                    <div id="seventh"><input type="text" name="bookNums" id="bookNums" size="35" value="31"/></div>
                    <div class="eighth"></div>
                    <div id="eighth"><input type="button" name="submit" value="保存" onclick="addBook()"/>
                        <input type="button" name="return" value="返回" onclick="goBack()"/></div>
                </form>
                </div>
                <br/>
            </div>
        <li id="tab2" class="tab_con">
            <div id="overall">
                <div class="split1">内容简介</div><textarea class="height1" id="abstract" ></textarea>
            <div class="tail">
            </div>
            <div class="Tail2">
                <ul>
                    <li class="Indentation1"><input type="button" value="保存" onclick="updateBookAbstract()"><input type="button" value="返回" onclick="goBack()"></li>
                </ul>
            </div>
        </li>
    </ul>
        <li id="tab3" class="tab_con">
            <div>
            <form id="bookPhoto" method="post" enctype="multipart/form-data">
                <div class="Link">
                书籍图片&nbsp;&nbsp;&nbsp;
                <img src="../image/default.jpg" width="200" height="200" id="photo"/>
                <input type="file" name="fileField" id="fileField"/>
            </div>
            <div class="Tail3">
                <ul>
                    <li class="Indentation1"><input name="button" type="button" class="btn" id="button" value="提交" onclick="updateBookPhoto()"/><input type="button" value="返回" onclick="goBack()"></li>
                </ul>
            </div>
            </form>
        </li>
    </ul>
</div>
</body>

</html>
